package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.DateUtils;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;
@SuppressWarnings("all")
/***
 * 中标库
 * @author jobs1127
 *
 */
public class BidDTO implements Uploadable,Serializable{
	/** 省份**/
	private String province="";
	/** 地市**/
	private String city="";
	/** 终端**/
	private String terminal="";
	/** 品种**/
	private String product="";
	/** 包装**/
	private String pack="";
	/** 中标价**/
	private String zbj="";
	/** 开始时间**/
	private String stime="";
	/** 结束时间**/
	private String etime="";
	/** 修订标识**/
	private String isUpdate = "E";
	/** id*/
	private String id = "";
	
	
	/***
	 * key = province+"-"+city+"-"+terminal+"-"+product+"-"+pack+"-"+stime+"-"+etime;
	 * 唯一标识this对象
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((city == null) ? 0 : city.hashCode());
		result = prime * result + ((etime == null) ? 0 : etime.hashCode());
		result = prime * result + ((pack == null) ? 0 : pack.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result + ((province == null) ? 0 : province.hashCode());
		result = prime * result + ((stime == null) ? 0 : stime.hashCode());
		result = prime * result + ((terminal == null) ? 0 : terminal.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BidDTO other = (BidDTO) obj;
		if (city == null) {
			if (other.city != null)
				return false;
		} else if (!city.equals(other.city))
			return false;
		if (etime == null) {
			if (other.etime != null)
				return false;
		} else if (!etime.equals(other.etime))
			return false;
		if (pack == null) {
			if (other.pack != null)
				return false;
		} else if (!pack.equals(other.pack))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		if (province == null) {
			if (other.province != null)
				return false;
		} else if (!province.equals(other.province))
			return false;
		if (stime == null) {
			if (other.stime != null)
				return false;
		} else if (!stime.equals(other.stime))
			return false;
		if (terminal == null) {
			if (other.terminal != null)
				return false;
		} else if (!terminal.equals(other.terminal))
			return false;
		return true;
	}
	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map,Set<Uploadable> set, int num) {
		/** 容纳错误消息的list */
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/** 容纳正确的，最终用于保存到数据库的数据 **/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);

		/***
		 * 过滤掉所有的空格
		 */
		String province = StringUtil.trimAll(this.getProvince());
		String city = StringUtil.trimAll(this.getCity());
		String terminal = StringUtil.trimAll(this.getTerminal());
		String product = StringUtil.trimAll(this.getProduct());
		String pack = StringUtil.trimAll(this.getPack());
		String zbj = StringUtil.trimAll(this.getZbj());
		String stime = StringUtil.trimAll(this.getStime());
		String etime = StringUtil.trimAll(this.getEtime());
		String isUpdate = StringUtil.trimAll(this.getIsUpdate());
		String id = StringUtil.trimAll(this.getId());

		/**
		 * 判断是否导入的模板是正确的模板
		 */
		if (num == 1) {
			if (!CollectionConstant.zbk_mb.get(0).contains(province) 
					|| !CollectionConstant.zbk_mb.get(1).contains(city)
					|| !CollectionConstant.zbk_mb.get(2).contains(terminal) 
					|| !CollectionConstant.zbk_mb.get(3).contains(product)
					|| !CollectionConstant.zbk_mb.get(4).contains(pack) 
					|| !CollectionConstant.zbk_mb.get(5).contains(zbj)
					|| !CollectionConstant.zbk_mb.get(6).contains(stime)
					|| !CollectionConstant.zbk_mb.get(7).contains(etime)
					|| !CollectionConstant.zbk_mb.get(8).contains(isUpdate)
					|| !CollectionConstant.zbk_mb.get(9).contains(id)) {
				MsgInfo msg = new MsgInfo(Constant.ERROR_MB, Constant.ERROR_MB, Constant.ERROR_MB,
						Constant.ERROR_NUM + num);
				mlist.add(msg);
				map.put("stop", mlist);
				return null;
			}
		} else {
			/***
			 * 获取项目上下文的共享数据
			 */
			ServletContext servletContext = ServletContextUtils.getServletContext();
			/**省份**/
			Set<String> provinces = (HashSet<String>) servletContext.getAttribute("provinces");
			/**地市**/
			Set<String> citys = (HashSet<String>) servletContext.getAttribute("citys");
			/**终端**/
			Set<String> cNames = (HashSet<String>) servletContext.getAttribute("cNames");
			/**中标库 重复项判断key **/
			String pzKeys = ((HashSet<String>) servletContext.getAttribute("pzKeys")).toString();

			/***
			 * 数据合格性校验
			 */
			if (!CollectionConstant.cuFlags.contains(isUpdate)) {
				MsgInfo msg = new MsgInfo(isUpdate, "修订标识", CollectionConstant.cuFlags.toString(),
						Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			if("E".equals(isUpdate)) {
				if(StringUtil.isBlank(id)) {
					MsgInfo msg = new MsgInfo(isUpdate,"修订标识有误或id有误","修订标识为E或空白时，id不能为空。id为空白时，修订标识只能是N",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			/***
			 * 省份参考【基础库】的省份
			 */
//			if (!provinces.contains(province)) {
//				MsgInfo msg = new MsgInfo(province, "省份", Constant.BASIC_DATA, Constant.ERROR_NUM + num);
//				mlist.add(msg);
//			}
			/**
			 * 地市允许为空，如果填写了，则参考【基础库】的地市
			 */
//			if(StringUtil.isNotBlank(city)) {
//				if (!citys.contains(city)) {
//					MsgInfo msg = new MsgInfo(city, "地市", Constant.BASIC_DATA, Constant.ERROR_NUM + num);
//					mlist.add(msg);
//				}
//			}
			/**
			 * 终端允许为空，如果填写了，则参考【注册库】的终端（入货乙方、出货甲方）
			 */
			if(StringUtil.isNotBlank(terminal)) {
				if (!cNames.contains(terminal)) {
					MsgInfo msg = new MsgInfo(terminal, "终端", Constant.CNAME_DATA, Constant.ERROR_NUM + num);
					mlist.add(msg);
				}
			}
			
			/**
			 * 判断品种
			 */
			if (!CollectionConstant.products.contains(product)) {
				MsgInfo msg = new MsgInfo(product, "品种", CollectionConstant.products.toString(),
						Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 判断包装
			 */
			if (!CollectionConstant.packs.contains(pack)) {
				MsgInfo msg = new MsgInfo(pack, "包装", CollectionConstant.packs.toString(), Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 标准测算价
			 */
			if (StringUtil.parseFloat(zbj) == null) {
				MsgInfo msg = new MsgInfo(zbj, "中标价", Constant.NUMBER, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			/**
			 * 判断日期
			 */
			if(DateUtils.parseDate(stime) == null) {
				MsgInfo msg = new MsgInfo(stime, "开始时间", Constant.DATE_FORMAT, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}
			if(DateUtils.parseDate(etime) == null) {
				MsgInfo msg = new MsgInfo(etime, "结束时间", Constant.DATE_FORMAT, Constant.ERROR_NUM + num);
				mlist.add(msg);
			}

			/***
			 * 判断是否在数据库中存在，排除重复项的可能
			 */
			if (isUpdate.equals("N")) {
				String key = province+"-"+city+"-"+terminal+"-"+product+"-"+pack+"-"+stime+"-"+etime;
				if (pzKeys.contains(key)) {
					MsgInfo msg = new MsgInfo(key, Constant.REPEATABLE, "对应了多个中标价，请检查", Constant.ERROR_NUM + num);
					mlist.add(msg);
				}
			} else if(isUpdate.equals("E")) {
				String key = province+"-"+city+"-"+terminal+"-"+product+"-"+pack+"-"+stime+"-"+etime;
				if (pzKeys.contains(key)) {
					key = key+"-"+id;
					if(!pzKeys.contains(key)) {
						MsgInfo msg = new MsgInfo(key, Constant.REPEATABLE, "对应了多个中标价，请检查", Constant.ERROR_NUM + num);
						mlist.add(msg);
					}
				}
			}

			/***
			 * 返回处理后的结果数据
			 */
			if (!mlist.isEmpty()) {
				map.put("fail" + num, mlist);
			} 
			/*** 把验证通过的合格数据返回 **/
			this.setAll(province, city, product, pack, terminal, stime, etime,zbj,isUpdate, id);
			//不是空行才添加
			if(StringUtil.isNotBlank(product)&&StringUtil.isNotBlank(pack)&&StringUtil.isNotBlank(zbj)) {
				set.add(this);
			}
			return this;
		}
		return null;
	}
	public void setAll(String province, String city, String product, String pack, String terminal, String stime,
			String etime, String zbj, String isUpdate, String id) {
		this.province = province;
		this.city = city;
		this.product = product;
		this.pack = pack;
		this.terminal = terminal;
		this.stime = stime;
		this.etime = etime;
		this.zbj = zbj;
		this.isUpdate = isUpdate;
		this.id = id;
	}
	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public String getPack() {
		return pack;
	}

	public void setPack(String pack) {
		this.pack = pack;
	}

	public String getTerminal() {
		return terminal;
	}

	public void setTerminal(String terminal) {
		this.terminal = terminal;
	}

	public String getStime() {
		return stime;
	}

	public void setStime(String stime) {
		this.stime = stime;
	}

	public String getEtime() {
		return etime;
	}

	public void setEtime(String etime) {
		this.etime = etime;
	}

	public String getZbj() {
		return zbj;
	}

	public void setZbj(String zbj) {
		this.zbj = zbj;
	}

	public String getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public BidDTO() {
		super();
	}

	public BidDTO(String province, String city, String product, String pack, String terminal, String stime,
			String etime, String zbj, String isUpdate, String id) {
		super();
		this.province = province;
		this.city = city;
		this.product = product;
		this.pack = pack;
		this.terminal = terminal;
		this.stime = stime;
		this.etime = etime;
		this.zbj = zbj;
		this.isUpdate = isUpdate;
		this.id = id;
	}

	@Override
	public String toString() {
		return "BidDTO [province=" + province + ", city=" + city + ", product=" + product + ", pack=" + pack
				+ ", terminal=" + terminal + ", stime=" + stime + ", etime=" + etime + ", zbj=" + zbj + ", isUpdate="
				+ isUpdate + ", id=" + id + "]";
	}
	@Override
	public String flag() {
		return this.isUpdate;
	}
	
}
